

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>


#include <float.h>

#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <vector>

#include "../include/postpro/postprocess.h"
#include "rk_debug.h"
#include "rk_defines.h"

#define FALSE		false
#define TRUE		true

static inline int64_t getCurrentTimeUs()
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec * 1000000 + tv.tv_usec;
}
int main(int argc, char *argv[] )
{

    FILE* rgbFile = NULL;
    rgbFile = fopen("/data/pic.rgb", "wb");
	if (rgbFile == NULL)
	{
		printf("cannot find rgb file\n");
		exit(1);
	}
	else
	{
		printf("The output yuv file\n" );
	}

    FILE* yuvFile = NULL;
    yuvFile = fopen("/data/test1.YUV", "rb");


    int frameWidth = 320;
    int frameHeight = 240;


    RK_U8* rBuf = NULL;
    RK_U8* gBuf = NULL;
    RK_U8* bBuf = NULL;
    RK_U8* yuvBuf = NULL;


    rBuf = (RK_U8*)malloc(frameWidth * frameHeight);
    gBuf = (RK_U8*)malloc(frameWidth * frameHeight);
    bBuf = (RK_U8*)malloc(frameWidth * frameHeight);
    
    yuvBuf = (RK_U8*)malloc(frameWidth * frameHeight * 3 / 2);
    if(yuvBuf == NULL || rBuf == NULL || gBuf == NULL || bBuf == NULL)
    {
        printf("no enought memory\n");
		exit(1);
    }
    fread(yuvBuf, 1, frameWidth*frameHeight* 3/2, yuvFile);
    for(int i=0; i<10; i++)
    {
        int64_t start_us = getCurrentTimeUs();

        yuv2rgb(frameWidth, frameHeight, yuvBuf, rBuf, gBuf, bBuf, FALSE);
        int64_t elapse_us = getCurrentTimeUs() - start_us;
        printf("%4d: Elapse Time = %.2fms, FPS = %.2f\n", i, elapse_us / 1000.f, 1000.f * 1000.f / elapse_us);

    }


    for (int i = 0; i < frameWidth * frameHeight; i++)
	{
		fwrite(bBuf+i, 1, 1, rgbFile);
		fwrite(gBuf+i, 1, 1, rgbFile);
		fwrite(rBuf+i, 1, 1, rgbFile);
	}
    fclose(rgbFile);
    fclose(yuvFile);


    return 0;
}